package com.hpjz.basicofing.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.hpjz.basicofing.bean.UserBean;
import com.hpjz.basicofing.dao.IUserDao;
import com.hpjz.basicofing.service.IUserService;
import com.hpjz.basicofing.web.vo.UserVO;
import com.hpjz.common.utils.CommUtils;
import com.hpjz.common.utils.DateUtil;

@Service("userService")
public class UserServiceImpl implements IUserService{
     
	@Resource
	private IUserDao userDao;
	

	@Override
	public boolean insertUser(UserVO vo) {
		UserBean bean = this.voToBean(vo);
		String sex = vo.getSex();
		if (StringUtils.isNotBlank(sex)) {
		   bean.setSex(Integer.valueOf(sex));
		}
		bean.setUid(CommUtils.getUUID());
		bean.setCreatetime(new Date());
		bean.setUpdatetime(new Date());
		//根据登录名查询是否存在，存在，提示
		UserBean search = new UserBean();
	    search.setLoginname(bean.getLoginname());
		UserBean existuser = this.userDao.findOne(search);
		if (existuser!=null) {
			return false;
		}else{
			this.userDao.insert(bean);
			return  true;
		}
		
	}

	@Override
	public boolean updateUser(UserVO vo) {
		UserBean bean = this.voToBean(vo);
		String sex = vo.getSex();
		
		if (StringUtils.isNotBlank(sex)) {
			bean.setSex(Integer.valueOf(sex));
		}
		bean.setUpdatetime(new Date());
		
		boolean isUpdate =  this.userDao.update(bean)>0;
		return isUpdate;
	}

	@Override
	public UserVO findUser(UserVO vo) {
		UserBean bean = this.voToBean(vo);
		UserBean result = this.userDao.findOne(bean);
		
		UserVO resultvo = null;
		if (result!= null) {
			resultvo = this.beanToVO(result);
		}
		return resultvo;
	}

	@Override
	public boolean deleteUser(UserVO vo) {
		UserBean bean = this.voToBean(vo);
		boolean isDelete =  this.userDao.delete(bean)>0;
		return  isDelete;
	}
	
	
	 /**
	  * 将vo转化为bean
	  * @param vo
	  * @return
	  */
	private UserBean voToBean(UserVO vo) {
		UserBean bean = new UserBean();
		BeanUtils.copyProperties(vo, bean);
		return bean;
	}
	
	 /**
	  * 将bean转化为vo
	  * @param vo
	  * @return
	  */
	private UserVO beanToVO(UserBean bean) {
		UserVO vo = new UserVO();
		BeanUtils.copyProperties(bean, vo);

		if (bean.getSex()!=null) {
		   vo.setSex(String.valueOf(bean.getSex()));
		}
		
		if (bean.getCreatetime() != null) {
			String create =  DateUtil.dateToStr(bean.getCreatetime());
			vo.setCreatetime(create);
		}

		if (bean.getUpdatetime() != null) {
			String update =  DateUtil.dateToStr(bean.getCreatetime());
			vo.setUpdatetime(update);
		}
		
		if (bean.getLastlogintime()!=null) {
			String loginTime =  DateUtil.dateToStr(bean.getLastlogintime());
			vo.setLastlogintime(loginTime);
		}
		
		if (bean.getIsonline()!=null) {
		    vo.setIsonline(String.valueOf(bean.getIsonline()));
		}

		return vo;
	}

	
	 /**
	  * 调整验证用户登录合法性
	  */
	@Override
	public UserVO userLogin(UserVO vo) {
		UserBean bean = this.voToBean(vo);
		UserBean rbean =  this.userDao.findOne(bean);
		UserVO  rvo = new UserVO();
		if (rbean!=null) {
			UserBean upbean = new UserBean();
		    upbean.setUid(rbean.getUid());
		    upbean.setLastlogintime(new Date());
		    //设置在线状态
		    upbean.setIsonline(1L);
		    this.userDao.update(upbean);
		    rvo = this.beanToVO(rbean);
		}
		
		return rvo;
	}

	@Override
	public void userLoginOut(UserVO vo) {
	   UserBean bean = this.voToBean(vo);
	   bean.setIsonline(0L);
	   this.userDao.update(bean);
	}

	@Override
	public List<UserVO> findAllUsers() {
		List<UserBean> beanList = this.userDao.findAll();
		List<UserVO> voList = new ArrayList<>();
		for (UserBean ubean : beanList) {
			UserVO vo = this.beanToVO(ubean);
			voList.add(vo);
		}
		
		return voList;
	}
    
 
}
